/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pos.Driving.Dashboard.Image;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.media.j3d.ImageComponent;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.*;
import pos.Driving.Train.Train;
import pos.MessageBox.MessageBox;
import pos.utils.Invokeable;
import pos.utils.ObjectFactory;
import pos.utils.Plane2D;

/**
 *
 * @author Administrator
 */
public class StationInfo extends TransformGroup implements Invokeable{
    private double destRef,timeRef;
    private double dest=0,time=0;
    private boolean isDestRefGreaterThanDest;
    private String stationName;
    private Plane2D planeDest,planeTime, planeStationName,planeTimeRef;
    private TransformGroup tgDest, tgTime, tgStationName, tgTimeRef;

    public StationInfo(Transform3D arg0, double destRef, double timeRef, String stationName) {
        super(arg0);
        this.destRef = destRef;
        this.timeRef = timeRef;
        this.stationName = stationName;
        this.setCapability(ALLOW_TRANSFORM_WRITE);
        initObjects();
    }

    public void setDest(double dest) {
        if (Math.round(this.dest)!=Math.round(dest)){
            this.dest = dest;
            planeDest.setTexture(makeInfoDestTexture());
        }else
             this.dest = dest;
    }
    
    /*public void printPoints(){
        long scoreTime=Math.abs(Math.round(time)-Math.round(timeRef))/2;
        long scoreDest=Math.abs(Math.round(dest)-Math.round(destRef))*2;
        long point=100-scoreTime-scoreDest;
        new pos.CommandMsgbox(
                new MessageBox(null, null, "运营任务完成", 
                "您的运营质量为"+point+"分!",
                new String[]{"重新开始", "返回主菜单"},
                new String[]{"ResetTrain", "ChangeToMainMenu"})).execute(null);
    }*/
    
    public void reset(){
        time=0;
    }

    public void setDestRef(double destRef) {
        this.destRef = destRef;
        if (destRef>dest)
            isDestRefGreaterThanDest=true;
        else
            isDestRefGreaterThanDest=false;
    }

    public void setStationName(String stationName) {
        this.stationName = stationName;
        planeStationName.setTexture(makeInfoStationNameTexture());
        
    }

    public void setTimeRef(double timeRef) {
        this.timeRef = timeRef;
        planeTimeRef.setTexture(makeInfoTimeTexture(true));
    }

    public void setTime(double time) {
        if (Math.round(this.time)!=Math.round(time)){
            this.time=time;
            planeTime.setTexture(makeInfoTimeTexture());
        }
    }
    
    public double getTimeDiff(){
        return timeRef-time;
    }
    public double getDestDiff(){
        double diff=destRef-dest;
        if (!isDestRefGreaterThanDest)
            diff=-diff;
        return diff;
    }
    
    @Override
    public void invoke(double Step) {
    }
    
    private void initObjects(){
        this.addChild(ObjectFactory.createTexturedSurface(
                1.2f, .5f, 0f, new Color3f(1f,1f,1f), 0f, 
                makeInfoBackTexture()));
        
        Transform3D t3d=new Transform3D();
        t3d.set(new Vector3d(-0.1,0.065,0));
        tgDest=new TransformGroup(t3d);
        planeDest=(Plane2D)ObjectFactory.createTexturedSurface(
                0.25f, 0.1f, 0f, new Color3f(1f,1f,1f), 0f, 
                makeInfoDestTexture());
        tgDest.addChild(planeDest);
        this.addChild(tgDest);
        
        t3d.set(new Vector3d(-0.07,-0.16,0));
        tgTime=new TransformGroup(t3d);
        planeTime=(Plane2D)ObjectFactory.createTexturedSurface(
                0.5f, 0.1f, 0f, new Color3f(1f,1f,1f), 0f, 
                makeInfoTimeTexture());
        tgTime.addChild(planeTime);
        this.addChild(tgTime);
        
        t3d.set(new Vector3d(0.10,-0.06,0));
        tgTimeRef=new TransformGroup(t3d);
        planeTimeRef=(Plane2D)ObjectFactory.createTexturedSurface(
                0.5f, 0.1f, 0f, new Color3f(1f,1f,1f), 0f, 
                makeInfoTimeTexture());
        tgTimeRef.addChild(planeTimeRef);
        this.addChild(tgTimeRef);
        
        t3d.set(new Vector3d(0.35,0.18,0));
        tgStationName=new TransformGroup(t3d);
        planeStationName=(Plane2D)ObjectFactory.createTexturedSurface(
                1.2f, 0.1f, 0f, new Color3f(1f,1f,1f), 0f,
                makeInfoStationNameTexture());
        tgStationName.addChild(planeStationName);
        this.addChild(tgStationName);
    }
    private Texture2D makeInfoDestTexture() {
        BufferedImage tabBuffer = new BufferedImage(64, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) tabBuffer.getGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2d.setFont(new Font("SimHei",Font.PLAIN,30));
        g2d.setPaint(Color.BLUE);
        if (isDestRefGreaterThanDest)
            g2d.drawString(String.valueOf(Math.round(destRef-dest)), 0, 30);
        else
            g2d.drawString(String.valueOf(-Math.round(destRef-dest)), 0, 30);
        ImageComponent2D ic2D = new ImageComponent2D(ImageComponent.FORMAT_RGBA, tabBuffer, true, false);
        Texture2D textureOut = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, 64, 32);
        textureOut.setImage(0, ic2D);
        return textureOut;
    }
    private Texture2D makeInfoTimeTexture(boolean isRef) {
        BufferedImage tabBuffer = new BufferedImage(128, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) tabBuffer.getGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2d.setFont(new Font("SimHei",Font.PLAIN,30));
        
        if (!isRef){
            g2d.setPaint(Color.ORANGE);
            g2d.drawString(timeToString(time), 0, 30);
        }else{
            g2d.setPaint(Color.GREEN);
            g2d.drawString(timeToString(timeRef), 0, 30);
        }
        ImageComponent2D ic2D = new ImageComponent2D(ImageComponent.FORMAT_RGBA, tabBuffer, true, false);
        Texture2D textureOut = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, 128, 32);
        textureOut.setImage(0, ic2D);
        return textureOut;
    }
    private Texture2D makeInfoTimeTexture() {
        return makeInfoTimeTexture(false);
    }
    private Texture2D makeInfoStationNameTexture() {
        BufferedImage tabBuffer = new BufferedImage(512, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) tabBuffer.getGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2d.setFont(new Font("SimHei",Font.PLAIN,30));
        g2d.setPaint(Color.RED);
        g2d.drawString(this.stationName, 0, 30);
        ImageComponent2D ic2D = new ImageComponent2D(ImageComponent.FORMAT_RGBA, tabBuffer, true, false);
        Texture2D textureOut = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, 512, 32);
        textureOut.setImage(0, ic2D);
        return textureOut;
    }

    private Texture2D makeInfoBackTexture() {
        BufferedImage tabBuffer = new BufferedImage(512, 128, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) tabBuffer.getGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2d.setFont(new Font("SimHei",Font.PLAIN,30));
        g2d.setPaint(Color.RED);
        g2d.drawString("下一任务:", 0, 30);
        g2d.setPaint(Color.BLUE);
        g2d.drawString("当前距离:         米", 0, 60);
        g2d.setPaint(Color.GREEN);
        g2d.drawString("图定完成时间 ", 0, 90);
        g2d.setPaint(Color.ORANGE);
        g2d.drawString("当前时间 ", 0, 120);
        ImageComponent2D ic2D = new ImageComponent2D(ImageComponent.FORMAT_RGBA, tabBuffer, true, false);
        Texture2D textureOut = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, 512, 128);
        textureOut.setImage(0, ic2D);
        return textureOut;
    }
    private String timeToString(double time){
        long t=Math.round(time);
        long hour, minute, second;
        hour = 12+ t/3600;
        minute = (t/60) % 60;
        second = t % 60;
        return hour+":"+digToString(minute)+":"+digToString(second);
    }
    
    private String digToString(long d){
        if (d<10) return "0"+d;
        else return String.valueOf(d);
    }
}
